package com.sefonsoft.dataanalysis.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSON;
import com.sefonsoft.dataanalysis.common.SpringContextHolder;
import com.sefonsoft.dataanalysis.model.News;
import com.sefonsoft.dataanalysis.service.impl.BaseService;
import com.sefonsoft.dataanalysis.service.impl.NewsService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsBchyczdBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsBqglBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsCpppBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsDtxxBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsGcsyBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsJyhfbaBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsJynybaBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsNjzskwhBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsNjzsycpxBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsSsqjcBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsZrhfbaBeanService;
import com.sefonsoft.dataanalysis.service.impl.snyt.NytCsZrnybaBeanService;
import com.sefonsoft.dataanalysis.utils.ClassUtils;

/**
 * 数据处理
 * @author xgj
 *
 */
@RequestMapping("data")
@RestController
public class DataAnalysisContorller {
	
	@Autowired
	private NewsService newsService;
	
	@Transactional
	@GetMapping("/createTableTest")
	public String createTest() throws IOException {
		//创建表
		newsService.createTmpTable(false);
		System.out.println("创建表成功");
		long l1 = System.currentTimeMillis();
		List<News> list = newsService.getDatasByHtml();
		System.out.println("数据解析成功");
		long l = System.currentTimeMillis();
	    if (list != null && !list.isEmpty()) {
	    		newsService.insert(list);
	    }
	    System.out.println("解析花费时间："+(System.currentTimeMillis()-l1));
	    System.out.println("入库花费时间："+(System.currentTimeMillis()-l));
		System.out.println("数据插入成功");
		return "创建表成功";
	}
	
	@GetMapping("/query")
	public String query() throws IOException {
		List<Map<String, Object>> list = newsService.queryForTen(); 
		return JSON.toJSONString(list);
	}
	
	@Transactional
	@GetMapping("/createTable")
	public String createTable() throws IOException, ClassNotFoundException {
		//创建表
		System.out.println("创建表开始");
		long l1 = System.currentTimeMillis();
		for (Class<?> c : ClassUtils.getAllAssignedClass(BaseService.class)) {
			char[] cs = c.getSimpleName().toCharArray();
	        cs[0]+=32;
			(SpringContextHolder.getBean(String.valueOf(cs),BaseService.class)).createTmpTable(false);
		}
		System.out.println("建表花费时间："+(System.currentTimeMillis()-l1));
		return "创建表成功";
	}

	@Transactional
	@GetMapping("/insertData")
	public String insertData() throws IOException, ClassNotFoundException {
	
		long lz = System.currentTimeMillis();
		for (Class<?> c : ClassUtils.getAllAssignedClass(BaseService.class)) {
			long l = System.currentTimeMillis();
			char[] cs = c.getSimpleName().toCharArray();
	        cs[0]+=32;
	        String str = String.valueOf(cs);
	        BaseService<?> bsService = (SpringContextHolder.getBean(String.valueOf(cs),BaseService.class));
	        List list = new ArrayList<>();
	        switch (bsService.getSourceType()) {
			case HTML:
				list = bsService.getDatasByHtml();
				break;
			case JSON:
				list = bsService.getDatasByJSON();
				break;
			default:
				break;
			}
			System.out.println("数据解析成功");
			System.out.println(str+"解析花费时间："+(System.currentTimeMillis()-l));
			long ll = System.currentTimeMillis();
			if (list != null && !list.isEmpty()) {
				  bsService.insert(list);
		    }
			System.out.println("数据入库成功");
			System.out.println(str+"解析花费时间："+(System.currentTimeMillis()-ll));
		}
		System.out.println("数据全部插入成功");
		System.out.println("总花费时间："+(System.currentTimeMillis()-lz));
		return "创建表成功";
	}

}
